package com.inspection.controller;

import com.alibaba.fastjson.JSON;
import com.inspection.dao.InspectionBasicDao;
import com.inspection.entity.InspectionEventDO;
import com.inspection.model.dto.InspectionStatisticsQueryDTO;
import com.inspection.model.vo.InspectionStaticsVO;
import com.inspection.service.InspectionStatisticsService;
import com.inspection.support.Wrapper;
import com.inspection.support.model.ItemList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaoqi
 * @since 2020-11-12
 * 统计服务
 *
 * 根据 考核类别 部门ID  考核周期
 * 考核周期 可以是月 年+【1，2，3，4，5，6，7，8，9，10，11，12】 ||   可以是季度  年+ 【1,2,3,4】  ||可以是年  年
 *
 *
 * 根据 考核类别 部门ID 筛选出匹配的考核条例
 * 再根据考核条例筛选出 对应时间的段内的考核事件做聚合
 *
 *
 * @// TODO: 2020/11/12  将查询参数作为一个key进行缓存
 * 
 */
@RestController
@Slf4j
public class InspectionStatisticsController {
    
    @Autowired
    private InspectionStatisticsService inspectionStatisticsService;

    @Autowired
    private InspectionBasicDao inspectionBasicDao;


    @RequestMapping("/inspect/statistics/query")
    public Wrapper<ItemList<InspectionStaticsVO>> statistics(@RequestBody InspectionStatisticsQueryDTO query) {
        try {
            return Wrapper.ok(ItemList.buildItems(inspectionStatisticsService.queryStatistics(query)));
        } catch (Exception e) {
            log.error("汇总异常,e={}",e);
            return Wrapper.error("400",e.getMessage());
        }
    }

//    @RequestMapping("/inspect/collect/query")
//    public Wrapper<ItemList<InspectionStaticsVO>> collectQuery(@RequestBody InspectionStatisticsQueryDTO query) {
//        return Wrapper.ok(ItemList.buildItems(inspectionStatisticsService.collectQuery(query)));
//    }
    @RequestMapping("/inspect/collect/test")
    public String collectQuery() {
        List<String> list = new ArrayList<>();
        list.add("月");
        list.add("年");
        return JSON.toJSONString(inspectionBasicDao.findByInspectionCycleIn(list));
    }



    @RequestMapping("/inspect/statistics/export")
    public void exportStatistics(HttpServletResponse response,InspectionStatisticsQueryDTO query) {
        inspectionStatisticsService.exportStatistics(query,response);
    }



}
